import request from '@/utils/request'
import { mapGetters } from 'vuex'
export default {
  components: {},
  props: [],
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        campusIds: undefined,
        id: undefined,
        ruleId: undefined,
        ruleTitle: undefined,
        points: undefined,
        memo: undefined,
        type: 'Artificial'
      },
      rules: {
        ruleId: [
          {
            required: true,
            message: '请选择规则',
            trigger: 'change'
          },
        ],
        points: [
          {
            required: true,
            message: '请输入积分值',
            trigger: 'blur'
          },
        ],
      },
      ruleOptions: []
    }

  },
  watch: {},
  created () {
  },
  computed: {
    ...mapGetters([
      'handleSelection'
    ]),
  },
  mounted () {
    request({
      url: `/api/campus/integral/rule/selector`,
      method: 'get'
    }).then(res => {
      this.ruleOptions = res.data.filter(f => f.type == 'Artificial');
    })
  },
  methods: {
    deleteSelection (index) {
      this.$store.dispatch('integral/handleSelectionDelete', index)
    },
    hanldRuleChange (value) {
      const rule = this.ruleOptions.find(
        (ele) => ele.id === value
      );
      this.dataForm.points = rule.points
      this.dataForm.ruleTitle = rule.fullName
    },
    init (id) {
      console.log(this.handleSelection)
      this.dataForm.id = id || '';
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields();
      })
    },
    handleClose(done) {
      this.$emit('refresh', true)
      done();
    },
    onClose () {
      this.visible = false;
      this.$emit('refresh', true)
    },
    dataFormSubmit () {
      if (this.handleSelection.length < 1) {
        this.$message.error('请先选择校区');
      } else {
        let ids = this.handleSelection.map(item => item.id);
        this.dataForm.campusIds = ids
        this.loading = true;
        this.$refs['elForm'].validate((valid) => {
          if (valid) {
            request({
              url: `/api/campus/integral/batch/points`,
              method: 'post',
              data: this.dataForm,
            }).then((res) => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.loading = false;
                  this.$emit('refresh', true)
                }
              })
            }).catch((err) => {
              this.loading = false;
            })
          }
        })
      }
    },
  }
}
